package com.sunda.spmsweb.ordercontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsorder.service.IShoppingScenarioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 请购场景表表头 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-03-01
 */
@RestController
@RequestMapping("/shoppingScenario")
@Api(tags = "请购场景接口", description = "请购场景接口")
public class ShoppingScenarioController {

    @Autowired
    IShoppingScenarioService iShoppingScenarioService;

    @RequestMapping("/getOrderScenario")
    @ApiOperation(value = "获取工厂请购场景（表头 分页）",notes = "获取指定条件下工厂请购场景分页信息。werks 工厂代码必输，[\"GF01\", \"GF02\"]；isDelete：0有效，1已删除；isDelete传空则忽略；",
            httpMethod = "POST")
    public ResponseResult getOrderScenario(@RequestParam(defaultValue = "1") int pageNo ,
                                               @RequestParam(defaultValue = "20") int pageSize,
                                           @RequestBody String[] werks, String isDelete) {
        try{
            return ResponseResult.success().add("shoppingScenarioList", iShoppingScenarioService.getScenarioList(pageNo, pageSize, werks, isDelete));
        } catch (Exception e){
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }

    @RequestMapping("/queryOrderScenario")
    @ApiOperation(value = "获取工厂请购场景（表头 分页）",notes = "获取指定条件下工厂请购场景分页信息。werks 工厂代码必输，isDelete：0有效，1已删除；isDelete传空则忽略；" +
    "{ \n"+
    "  \"authorName\": \"张坤鹏\", \n"+
    "  \"spmsId\": \"GF03-001\", \n"+
    "  \"isDelete\": \"0\", \n"+
    "  \"pageNo\": 1, \n"+
    "  \"pageSize\": 20, \n"+
    "  \"scenarioName\": \"模板名称\", \n"+
    "  \"userId\": \"999904\", \n"+
    "  \"werks\": \"GF03\"\n"+
    "} \n",
            httpMethod = "POST")
    public ResponseResult queryOrderScenario(@RequestBody JSONObject queryObj) {
        try{
            return ResponseResult.success().add("shoppingScenarioList", iShoppingScenarioService.queryScenarioList(queryObj));
        } catch (Exception e){
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }

    @RequestMapping("/getOrderScenarioDtl")
    @ApiOperation(value = "获取工厂请购场景（行项目 不分页）",notes = "获取指定条件下工厂请购场景详情信息。传入请购场景表头 uuid = 9463644cb2a941c881e88c76d12b62fw。",
            httpMethod = "POST")
    public ResponseResult getOrderScenarioDtl(String uuid) {
        try{
            return ResponseResult.success().add("shoppingScenarioDtl", iShoppingScenarioService.getScenarioDtlList(uuid));
        } catch (Exception e){
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }

    @RequestMapping("/createScenario")
    @ApiOperation(value = "创建工厂请购场景及行项目",notes = "创建工厂请购场景及行项目，传入json字符串参考：\n" +
            "场景编号规则：工厂代码 werks + 序列号，如 GF01-1001\n" +
            "{\n" +
            "\t\"shoppingScenario\": {\n" +
            "\t\t\"scenarioDesc\": \"GF01xx电机维修场景描述\",\n" +
            "\t\t\"machineDesc\": \"设备描述\",\n" +
            "\t\t\"createUserid\": \"20001\",\n" +
            "\t\t\"werks\": \"GF01\",\n" +
            "\t\t\"machineNo\": \"machine no\",\n" +
            "\t\t\"scenarioName\": \"GF01xx电机维修场景\",\n" +
            "\t\t\"remarks\": null\n" +
            "\t},\n" +
            "\t\"shoppingScenarioDtl\": [{\n" +
            "\t\t\t\"ssdRowId\": 1,\n" +
            "\t\t\t\"quantity\": 5,\n" +
            "\t\t\t\"longContractCode\": null,\n" +
            "\t\t\t\"materialNo\": \"130014340\",\n" +
            "\t\t\t\"brand\": null\n" +
            "\t\t},\n" +
            "\t\t{\n" +
            "\t\t\t\"ssdRowId\": 2,\n" +
            "\t\t\t\"quantity\": 6,\n" +
            "\t\t\t\"longContractCode\": null,\n" +
            "\t\t\t\"materialNo\": \"130011913\",\n" +
            "\t\t\t\"brand\": null\n" +
            "\t\t}\n" +
            "\t]\n" +
            "}",
            httpMethod = "POST")
    public ResponseResult createScenario(@RequestBody JSONObject jsonObject) {
        try{
            return iShoppingScenarioService.createScenario(jsonObject);
        } catch (Exception e){
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }

    @RequestMapping("/updateScenario")
    @ApiOperation(value = "更新工厂请购场景表头",notes = "更新工厂请购场景，uuid 作为唯一主键不可修改，werks、scenarioId不可修改，传入json字符串参考：\n" +
            "{\n" +
            "\t\"uuid\": \"429ecfbda9ba43e8a03b90945300f568\",\n" +
            "\t\"scenarioName\": \"GF01xx电机维修场景\",\n" +
            "\t\"scenarioDesc\": \"GF01xx电机维修场景描述\",\n" +
            "\t\"machineNo\": \"machine no\",\n" +
            "\t\"machineDesc\": \"设备描述\",\n" +
            "\t\"remarks\": null,\n" +
            "\t\"isDelete\": \"0\",\n" +
            "\t\"updateUser\": \"20001\"\n" +
            "}" ,
            httpMethod = "POST")
    public ResponseResult updateScenario(@RequestBody JSONObject jsonObject) {
        try{
            return iShoppingScenarioService.updateScenario(jsonObject);
        } catch (Exception e){
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }

    @RequestMapping("/updateScenarioDtl")
    @ApiOperation(value = "更新工厂请购场景明细",notes = "更新工厂请购场景明细，uuid 作为唯一主键不可修改，传入json字符串参考：\n" +
            "{\n" +
            "\t\"uuid\": \"9463644cb2a941c881e88c76d12b62fw\",\n" +
            "\t\"shoppingScenarioDtl\": [{\n" +
            "\t\t\t\"ssdRowId\": 1,\n" +
            "\t\t\t\"quantity\": 5,\n" +
            "\t\t\t\"longContractCode\": null,\n" +
            "\t\t\t\"materialNo\": \"130014340\",\n" +
            "\t\t\t\"brand\": null\n" +
            "\t\t},\n" +
            "\t\t{\n" +
            "\t\t\t\"ssdRowId\": 2,\n" +
            "\t\t\t\"quantity\": 6,\n" +
            "\t\t\t\"longContractCode\": null,\n" +
            "\t\t\t\"materialNo\": \"130011913\",\n" +
            "\t\t\t\"brand\": null\n" +
            "\t\t}]\n" +
            "}" ,
            httpMethod = "POST")
    public ResponseResult updateScenarioDtl(@RequestBody JSONObject jsonObject) {
        try{
            return iShoppingScenarioService.updateScenarioDtl(jsonObject);
        } catch (Exception e){
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }


    @RequestMapping("/deleteScenarios")
    @ApiOperation(value = "删除多个请购场景",notes = "删除多个请购场景示例参数：[\"239ecfbda9ba43e8a03b90945300f568\", \"429ecfbda9ba43e8a03b90945300f568\"]",
            httpMethod = "POST")
    public ResponseResult deleteScenarios(@RequestBody String[] scenarioUuid) {
        try{
            return iShoppingScenarioService.deleteScenarios(scenarioUuid);
        } catch (Exception e){
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }
}
